package com.example.service.impl;

import com.example.entity.B_file; 
import com.example.mapper.B_fileMapper;
import com.example.service.IB_fileService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 文件表 服务实现类
 * </p>
 *
 * @author Funkye
 * @since 2019-04-10
 */
@Service
@Transactional
public class B_fileServiceImpl extends ServiceImpl<B_fileMapper, B_file> implements IB_fileService {

	@Override
	public B_file getOne(B_file images, String... string) {
		// TODO Auto-generated method stub
		QueryWrapper<B_file> ew = new QueryWrapper<B_file>();
		ew.setEntity(images);
		if (string != null && string.length > 0) {
			ew.select(string);
		}
		ew.last("LIMIT 1");
		return getOne(ew);
	}

	@Override
	public B_file saveBfile(B_file images) {
		// TODO Auto-generated method stub
		try {
			save(images);
			return images;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public List<B_file> listOrById(String... fileIds) {
		// TODO Auto-generated method stub
		if (fileIds == null || fileIds.length <= 0) {
			return null;
		}
		QueryWrapper<B_file> ew = new QueryWrapper<B_file>();
		ew.select("id", "url","middleUrl","litterUrl");
		Integer id = 0;
		for (int i = 0; i < fileIds.length; i++) {
			id=Integer.valueOf(fileIds[i]);
			if (id > 0) {
				ew.eq("id", id);
				if (i + 1 < fileIds.length) {
					ew.or();
				}
			}
		}
		return list(ew);
	}

	@Override
	public IPage<B_file> page(Integer start, Integer size, String odb, boolean key, B_file b_file, String... strings) {
		// TODO Auto-generated method stub
		QueryWrapper<B_file> qw = new QueryWrapper<B_file>();
		if (strings != null && strings.length > 0) {
			qw.select(strings);
		}
		qw.setEntity(b_file);
		if (odb != null && odb.trim().length() > 0) {
			if (key == true) {
				qw.orderByDesc(odb);
			} else {
				qw.orderByAsc(odb);
			}
		} else {
			if (key == true) {
				qw.orderByDesc("id");
			} else {
				qw.orderByAsc("id");
			}
		}
		Page<B_file> page = new Page<B_file>(start, size);
		return page(page, qw);
	}

}
